//
//  ApplicationController.m
//  AFCalendarControl
//
//  Created by Keith Duncan on 26/06/2007.
//  Copyright 2007 thirty-three. All rights reserved.
//

#import "ApplicationController.h"

#import "AFCalendarControl.h"
#import "AFSegmentedWeekControl.h"

@interface ApplicationController ()
@property (readwrite, retain) NSMutableDictionary *enabledDays;
@property (readwrite, retain) NSDate *selectedDate;
@end

@implementation ApplicationController

@synthesize weekView;
@synthesize enabledDays=_enabledDays;

@synthesize calendarView;
@synthesize selectedDate=_selectedDate;

- (id)init {
	self = [super init];
	if (self == nil) return nil;
	
	self.selectedDate = [NSDate date];
	
	self.enabledDays = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
						[NSNumber numberWithBool:NO], AFKeyForWeekday(SUNDAY),
						[NSNumber numberWithBool:YES], AFKeyForWeekday(MONDAY),
						[NSNumber numberWithBool:YES], AFKeyForWeekday(TUESDAY),
						[NSNumber numberWithBool:YES], AFKeyForWeekday(WEDNESDAY),
						[NSNumber numberWithBool:YES], AFKeyForWeekday(THURSDAY),
						[NSNumber numberWithBool:YES], AFKeyForWeekday(FRIDAY),
						[NSNumber numberWithBool:NO], AFKeyForWeekday(SATURDAY),
						nil];
	
	return self;
}

- (void)dealloc {
	self.enabledDays = nil;
	self.selectedDate = nil;
	
	[super dealloc];
}

- (void)_changeSelectedDate:(NSTimer *)timer {
	self.selectedDate = [self.selectedDate dateByAddingDays:1];
}

- (void)awakeFromNib {
	[self.calendarView bind:@"selectedDate" toObject:self withKeyPath:@"selectedDate" options:nil];
}

@end

@implementation ApplicationController (DataSource)

- (BOOL)weekview:(AFSegmentedWeekControl *)view isEnabledForDay:(AFWeekday)day {
	return [[self.enabledDays objectForKey:AFKeyForWeekday(day)] boolValue];
}

- (void)weekview:(AFSegmentedWeekControl *)view setEnabled:(BOOL)enabled forDay:(AFWeekday)day {
	[self.enabledDays setValue:[NSNumber numberWithBool:enabled] forKey:AFKeyForWeekday(day)];
}

@end
